package daily;

/**
 * @author MtRider
 * @version 1.0
 * @description 最长公共前缀
 * <p>
 * 输入：strs = ["flower","flow","flight"]
 * 输出："fl"
 * @GiteeAndGithub MtRider
 * @date 2022/4/24 16:32
 */
public class demo15 {
    public static void main(String[] args) {
        String[] strs = {"flower", "flow", "flight"};
        System.out.println(longestCommonPrefix(strs));
        /*String s="admin";
        String s1="ad";
        System.out.println(s.indexOf(s1));*/
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        String result = strs[0];
        for (String str : strs) {
            while (str.indexOf(result) != 0) {
                result = result.substring(0, result.length() - 1);
                if (result.length() == 0) {
                    return "";
                }
            }
        }
        return result;
    }


}
